package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class FindServer
 */
@WebServlet("/FindServer")
public class FindServer extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String newServerLocation = "TIMISOARA";
    private final String timisoaraHost = "http://localhost:8080/TIMISOARARoadRunner/";
    private final String bucurestiHost = "http://localhost:8081/BUCURESTIRoadRunner/";
    private final String clujHost = "http://localhost:8082/CLUJRoadRunner/";
    private List<String> locationsList;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindServer() {
        super();
//        locationsList = new ArrayList<String>();
//        locationsList.add("TIMISOARA");
//        locationsList.add("BUCURESTI");
//        locationsList.add("CLUJ");
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text");
		String actualLocation = request.getParameter("serverlocation");
		if(actualLocation.equalsIgnoreCase("TIMISOARA")){
			newServerLocation = timisoaraHost;
		} else if(actualLocation.equalsIgnoreCase("BUCURESTI")){
			newServerLocation = bucurestiHost;
		} else if (actualLocation.equalsIgnoreCase("CLUJ")){
			newServerLocation = clujHost;
		} else {
			newServerLocation = "Out of coverage area!";
		}
		
//		if(locationsList.contains(actualLocation)){
//			serverName = actualLocation;
//		} else {
//			serverName = "Out of coverage area!";
//		}
		
		response.getWriter().write(newServerLocation);
		response.sendRedirect(newServerLocation);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
